import { ref, computed, watch } from 'vue'
import { defineStore } from 'pinia'
import {useEnvConfig} from '@/config'

// 配置数据
export const useDownloadPath = defineStore('downloadPath', () => {
  const {ENV_CONFIG} = useEnvConfig()
  const downloadPath = ref('');//下载路径
  const isDefaultPath = ref(false);//是否是默认路径
  const closeAndHide = ref(true);//关闭的时候是隐藏
  function setDownloadPath(path: string) {//设置默认路径
    downloadPath.value = path;
  }
  function setIsDefault(_default: boolean) {// 设置是否是默认的
    isDefaultPath.value = _default;
  }
  const setCloseAndHide = (quit: boolean)=>{//关闭的时候，是否退出
    closeAndHide.value = quit; 
  }

  watch(downloadPath, (value) => {
    localStorage.setItem(`${ENV_CONFIG.DEFAULT_PATH_NAME}_download_path`, value)
  });

  watch(isDefaultPath, (value) => {
    localStorage.setItem(`${ENV_CONFIG.DEFAULT_PATH_NAME}_download_path_is_default`, JSON.stringify(value))
  });

  watch(closeAndHide, (value) => {
    localStorage.setItem(`${ENV_CONFIG.DEFAULT_PATH_NAME}_close_and_quit`, JSON.stringify(value))
  })

  return { downloadPath, isDefaultPath,closeAndHide,setDownloadPath,setIsDefault,setCloseAndHide }
})
